package com.android.mios.adapter;

import java.io.File;
import java.util.ArrayList;
import com.android.mios.R;
import com.android.mios.util.AsyncImageLoader;
import com.android.mios.util.Global;
import com.android.mios.util.AsyncImageLoader.ImageCallback;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class SystemSwitchSkinGridViewAdapter extends BaseAdapter {

	private ArrayList<Object> list;
	//private Context context;
	private int resource;
	private LayoutInflater mInflater;
	//private Resources res;

	private static final int SKIN_ADD_BG = R.drawable.skin_add_bg;
	private static final int SKIN_ADD_ICON = R.drawable.skin_add_icon;
	private static final String ADD = "ADD";
//	private static final int DEFAUT_COUNT = 12;

	private AsyncImageLoader asyncImageLoader;
	private GridView mGridView;

	// public ArrayList<Boolean> isDeleteIconShow;

	public SystemSwitchSkinGridViewAdapter(Context context,
			ArrayList<Object> list, int resource, GridView mGridView) {

		//this.context = context;
		this.list = list;
		this.resource = resource;

		//this.res = context.getResources();

		this.list.add(ADD);
		// isDeleteIconShow = new ArrayList<Boolean>();
		// for (int i = 0; i < list.size(); i++)
		// isDeleteIconShow.add(false);

		mInflater = LayoutInflater.from(context);

		asyncImageLoader = new AsyncImageLoader(context);

		this.mGridView = mGridView;

	}

	public void add(String image) {
		this.list.add(-1 + this.list.size(), image);
		// isDeleteIconShow.add(-1 + this.list.size(), false);
	}

	public void del(String image, int position) {
		this.list.remove(image);
		// isDeleteIconShow.remove(position);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	static class ViewHolder {

		ImageView iv_bg;
		ImageView iv_delete;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {

			convertView = mInflater.inflate(resource, null);
			holder = new ViewHolder();
			holder.iv_bg = (ImageView) convertView
					.findViewById(R.id.img_switch_skin);
			holder.iv_delete = (ImageView) convertView
					.findViewById(R.id.img_delete_bg);
			convertView.setTag(holder);

		} else
			holder = (ViewHolder) convertView.getTag();

		final Object name = list.get(position);
		holder.iv_bg.setTag(name);

		if (position == list.size() - 1) {
			holder.iv_bg.setScaleType(ScaleType.CENTER);
			holder.iv_bg.setBackgroundResource(SKIN_ADD_BG);
			holder.iv_bg.setImageResource(SKIN_ADD_ICON);

		} else {
			holder.iv_bg.setBackgroundColor(0xffffffff);
			holder.iv_bg.setScaleType(ScaleType.FIT_XY);
			Bitmap cachedImage = asyncImageLoader.loadDrawable(name,
					new ImageCallback() {

						@Override
						public void imageLoaded(Bitmap imageDrawable,
								Object name) {
							// TODO Auto-generated method stub
							ImageView imageViewByTag = (ImageView) mGridView
									.findViewWithTag(name);
							if (imageViewByTag != null) {
								imageViewByTag.setImageBitmap(imageDrawable);
							}
						}
					});

			if (cachedImage == null) {
				holder.iv_bg.setImageBitmap(null);
			} else {
				holder.iv_bg.setImageBitmap(cachedImage);
			}
		}
		// if (position == list.size() - 1) {
		//
		// holder.iv.setBackgroundResource(SKIN_ADD_BG);
		// holder.iv.setImageResource(SKIN_ADD_ICON);
		//
		// } else {
		//
		// BitmapFactory.Options opts = new BitmapFactory.Options();
		// opts.inJustDecodeBounds = false;
		// // opts.inSampleSize=8;
		// Bitmap bitmap = BitmapFactory.decodeResource(res, (Integer) name,
		// opts);
		//
		// if (position < DEFAUT_COUNT) {
		//
		// holder.iv.setBackgroundDrawable(Utils
		// .bitmapToDrawableByBD(bitmap));
		// holder.iv.setImageBitmap(null);
		//
		// }
		//
		// }
		// if (isDeleteIconShow.get(position))
		// holder.iv_delete.setVisibility(View.VISIBLE);
		// else
		// holder.iv_delete.setVisibility(View.GONE);
		holder.iv_delete.setVisibility(View.GONE);
		holder.iv_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				del((String) name, position);

				File file = new File(Global.getInstance().getFileForPath(
						Global.getInstance().getSkinPath()), (String) name);
				
				if (file != null)
					file.delete();

				notifyDataSetChanged();
			}
		});
		return convertView;
	}
}
